package com.zxq.web.nettyhandler;
import com.zxq.exchange.enums.EFrame;
import com.zxq.exchange.model.ExchangeDTO;
import com.zxq.factory.decoder.impl.HexDecoder;
import com.zxq.tcp.cache.TcpChannelCache;
import com.zxq.tcp.handler.dispatch.TCPDispatchHandler;
import com.zxq.tcp.util.TranscodeUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

@Slf4j
public class TCPModBusRegistrationHandler extends TCPDispatchHandler {


    public TCPModBusRegistrationHandler() {

    }

    @Override
    public boolean isThisFrame() {
        if(in.readByte() == 0x7E) {
            return true;
        }
        in.resetReaderIndex();
        return false;
    }

    HexDecoder hexDecoder = new HexDecoder();
    /**
     * 返回空，不处理
     * @return
     */
    @Override
    protected ExchangeDTO decodeExchangeDTO() {
        List<Byte> byteList = new ArrayList<>();
        Byte item = 0x7F;
        do{
            item = in.readByte();
            byteList.add(item);
        } while (item != 0x7F);
        byte[] srcBuff = new byte[byteList.size() - 1];
        byte[] dstBuff = TranscodeUtils.transcodeDecoder(srcBuff);
        String devId = hexDecoder.decoder(dstBuff);
        TcpChannelCache.putNettyChannel(devId,channel);
        log.warn(String.format("dev:{0} register",devId));
        return null;
    }

    @Override
    protected Object produceFrame(String cmd_id, byte[] data) {

        return null;
    }

    @Override
    protected EFrame getTcpFrame() {

        return null;
    }
}
